package ajax17;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class AddPostServlet
 */
public class AddPostServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddPostServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String userid;
		String nickname;
		String title;
		String content;
		
		userid = request.getParameter("userid");
		nickname = request.getParameter("userid");
		title = request.getParameter("title");
		content = request.getParameter("content");
		
		if (userid != null){
			userid = request.getParameter("userid");
			nickname = request.getParameter("userid");
			title = request.getParameter("title");
			content = request.getParameter("content");
			Post p = new Post(userid, nickname, title, content);
			HttpSession session = request.getSession();
			session.setAttribute("p", p);
			
			System.out.println("p.save() = " + p.save());
			if (p.save()){
				// Redirect
				response.sendRedirect("success.jsp");
			}else{
				//
				response.sendRedirect("add_post.jsp");
			}
			
		}	
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
